package com.zengde.ai_sandbox.config;


import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.*;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class DaobaoLCAiConfig {
    @Autowired
    public OpenAiEmbeddingModel openAiEmbeddingModel;

    public interface DaobaoAssistant {
        String chat(String message);

        // 流式响应
        /**
         * 传入message,进行对话
         * @param message
         * @return
         */
        TokenStream stream(String message);

        /*@SystemMessage("""
                          你扮演一名购买衣服的客户，男，35岁，有点挑剔。需要对客服进行训练，一次只能问一个问题，问5轮，所有问题回答完后进行打分，中途不要打分。
                今天的日期是 {{current_date}}.
                  """)
        TokenStream stream(@UserMessage String message, @V("current_date") String currentDate);*/


        /*TokenStream stream(@UserMessage String message,@V("current_date") String currentDate);*/
    }


    /**
     * @param doubaoChatModel
     * @param douBaostreamingChatModel
     * @return
     */


    @Bean
    public DaobaoAssistant assistant(ChatModel doubaoChatModel,
                                     @Qualifier("openAiStreamingChatModel") StreamingChatModel douBaostreamingChatModel,
                                     EmbeddingStore embeddingStore) {
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(50);

        /*dev.langchain4j.data.message.SystemMessage systemMessage = dev.langchain4j.data.message.SystemMessage
                .systemMessage(getPrompt());
        chatMemory.add(systemMessage);*/

        ContentRetriever contentRetriever = EmbeddingStoreContentRetriever.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(openAiEmbeddingModel)
                .maxResults(5) // 最相似的5个结果
                .minScore(0.6) // 只找相似度在0.6以上的内容
                .build();


        DaobaoAssistant assistant = AiServices.builder(DaobaoAssistant.class)
                .chatModel(doubaoChatModel)
                .streamingChatModel(douBaostreamingChatModel)
                .chatMemory(chatMemory)
                /*.contentRetriever(contentRetriever)*/
                .build();

        return assistant;
    }


    @Bean
    public EmbeddingStore embeddingStore() {
        return new InMemoryEmbeddingStore();
    }






}
